<?php

require_once("dao/Dao.php");

/**
 * DAO de l'objet CorrespondanceDestinationDao.
 */
class CorrespondanceDestinationDao extends Dao {

    protected static $tableName = "correspondance_destinations";

    /**
     * Retourne la destination pour un numéro de téléphone donné
     * @param type $phoneNumber
     * @return type
     */
    public static function findByPhoneNumber($phoneNumber, $type) {
        if (!in_array($type, array('voip', 'vga'))) {
            return false;
        }
        $q = "SELECT 
              cd.destination,
              cd.digits,
              tarif.tarif_1,
              tarif.tarif_2,
              tarif.tarif_3,
              tarif.tarif_4,
              tarif.tarif_5
              FROM correspondance_destinations AS cd
              INNER JOIN destinations_tarifs_" . $type . " AS temp ON cd.id = temp.id_destination 
              INNER JOIN " . $type . "_table_prix AS tarif ON temp.id_prix = tarif.id
              WHERE LOCATE( digits, '$phoneNumber', 1) = 1
              ORDER BY LENGTH(digits) DESC
              LIMIT 1";
        return parent::queryOne($q);
    }

}
